<%@ include file="../common/header.jsp" %>

<div class="content">
    <%@ include file="../common/top_nav.jsp" %>

    <div class="container mt-4">
        <h2>设备巡检记录管理</h2>

        <!-- 新增按钮 -->
        <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#inspectionModal"
                onclick="prepareAddInspection()">新增巡检记录
        </button>

        <!-- 巡检记录列表 -->
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>设备ID</th>
                <th>巡检日期</th>
                <th>巡检结果</th>
                <th>巡检人员</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach var="inspection" items="${list}">
                <tr>
                    <td>${inspection.deviceId}</td>
                    <td>${inspection.inspectionDate}</td>
                    <td>${inspection.inspectionResult}</td>
                    <td>${inspection.inspectorName}</td>
                    <td>
                        <button class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#inspectionModal"
                                onclick="prepareEditInspection(${inspection.id})">编辑</button>
                        <a href="<c:url value='/inspection/delete/${inspection.id}'/>" class="btn btn-danger btn-sm"
                           onclick="return confirm('确定要删除这条记录吗？')">删除</a>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</div>

<!-- 新增/编辑模态框 -->
<div class="modal fade" id="inspectionModal" tabindex="-1" aria-labelledby="inspectionModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form id="inspectionForm" action="<c:url value='/inspection/add'/>" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="inspectionModalLabel">巡检记录</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="关闭"></button>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="id" name="id">

                    <div class="mb-3">
                        <label for="deviceId" class="form-label">设备ID</label>
                        <input type="number" class="form-control" id="deviceId" name="deviceId" required>
                    </div>

                    <div class="mb-3">
                        <label for="inspectionDate" class="form-label">巡检日期</label>
                        <input type="date" class="form-control" id="inspectionDate" name="inspectionDate" required>
                    </div>

                    <div class="mb-3">
                        <label for="inspectionResult" class="form-label">巡检结果</label>
                        <select class="form-select" id="inspectionResult" name="inspectionResult" required>
                            <option value="">请选择</option>
                            <option value="合格">合格</option>
                            <option value="不合格">不合格</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label for="inspectorName" class="form-label">巡检人员</label>
                        <input type="text" class="form-control" id="inspectorName" name="inspectorName" required>
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="submit" class="btn btn-success">保存</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>

<%@ include file="../common/footer.jsp" %>

<script>
    function prepareAddInspection() {
        document.getElementById("inspectionModalLabel").innerText = "新增巡检记录";
        document.getElementById("inspectionForm").action = "<c:url value='/inspection/add'/>";
        document.getElementById("id").value = "";
        document.getElementById("deviceId").value = "";
        document.getElementById("inspectionDate").value = "";
        document.getElementById("inspectionResult").value = "";
        document.getElementById("inspectorName").value = "";
    }

    function prepareEditInspection(id) {
        fetch("<c:url value='/inspection/get/'/>" + id, {
            headers: {
                "Accept": "application/json"
            }
        })
            .then(res => {
                if (!res.ok) throw new Error("加载失败: " + res.status);
                return res.json();
            })
            .then(data => {
                document.getElementById("inspectionModalLabel").innerText = "编辑巡检记录";
                document.getElementById("inspectionForm").action = "<c:url value='/inspection/update'/>";
                document.getElementById("id").value = data.id;
                document.getElementById("deviceId").value = data.deviceId;
                document.getElementById("inspectionDate").value = data.inspectionDate;
                document.getElementById("inspectionResult").value = data.inspectionResult;
                document.getElementById("inspectorName").value = data.inspectorName;
            })
            .catch(err => {
                console.error(err);
                alert("获取巡检记录失败！");
            });
    }
</script>
